class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }



    componentDidMount() {
        if(Constant.SWITCH_VCONSOLE){
            new VConsole();
        }
        let openid = localStorage.getItem('openid')
        console.log(openid,'local--openid');
        let code = GetQueryString('code');
        let id = GetQueryString('id');
        localStorage.setItem('doorId',id)
        if(openid!="null"&&openid!=''&&openid!=null){
            this.checkuser(openid)
                console.log('有记录');
            // getuser(openid,res1=>{
                
            // })
        }else{
            if(code=='null'){
               
                let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa29d77bb2e0c40bb&redirect_uri=http://techcherry.cn/yyfzph5/welcome.html?response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
                    
                window.location.href=url
            }else{
                wxlogin(code,res=>{
                    let openid = JSON.parse(res).data.openid;
                    localStorage.setItem('openid',openid)  
                   this.checkuser(openid)
                    //console.log(JSON.parse(res).data.openid,'wxlogin--openid');
                    this.setState({showbtn:!0,openid})
                   // localStorage.setItem('openid',res.data.wxUserInfo.openid)  
                   // window.location.href='./dc.html?id='+id
                })
            }
        }


    }

    checkuser(openid){
        getuser(openid,res=>{
            if(res.issuccess==1){
                if(res.user==null){
                    window.location.href='./tomember.html?id='+res.user.id
                }
                if(res.user.name ==null){
                   window.location.href='./tomember.html?id='+res.user.id
                }else{
                    localStorage.setItem('user',JSON.stringify(res.user))
                    window.location.href ='./main.html'
                }
            }
        })
    }



    pay(){
        let{openid} = this.state;
        praypay(openid,res=>{
            console.log(res,'resres');
            let data = JSON.parse(res.res)

            this.requestpay(data)
        })
        
    }



    requestpay(data){
       function onBridgeReady(){

        let sign = md5(`appId=wx58c6c13c539ee100&nonceStr=${data.nonce_str}&package=${data.package}&signType=MD5&timeStamp=${data.timeStamp}&key=Y3A7lWq2Xsd7ScCTSp8QUqLiWELxHCl7`).toUpperCase()
            WeixinJSBridge.invoke(
               'getBrandWCPayRequest', {
                  "appId":"wx58c6c13c539ee100",     //公众号ID，由商户传入     
                  "timeStamp":data.timeStamp,         //时间戳，自1970年以来的秒数     
                  "nonceStr":data.nonce_str, //随机串     
                  "package":data.package,     
                  "signType":"MD5",         //微信签名方式：     
                  "paySign":sign //微信签名 
               },

               function(res){
               if(res.err_msg == "get_brand_wcpay_request:ok" ){
               // 使用以上方式判断前端返回,微信团队郑重提示：
                     //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
               } 
            }); 
         }
         if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
         }else{
            onBridgeReady();
         }
    }
   


  
    render() {
        let{showbtn} = this.state
        return (
            <div id="app">
                {
                    showbtn ? 
                    <div onClick={this.pay.bind(this)}>
                           立即支付     

                    </div> :null
                }
            </div>
        )
    }
}

ReactDOM.render(
    <div>
        {/* <TopLine /> */}
        <div id='body'>
            <Main />
        </div>
    </div>
    // <Barrage />
    , $('.container')[0]
)